package com.lancoo.znbkxx.teacher.adapter

import android.annotation.SuppressLint
import android.media.MediaMetadataRetriever
import android.os.AsyncTask
import android.os.Build
import android.os.Handler
import android.os.Message
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.tools.ScreenUtils
import kotlinx.android.synthetic.main.item_recording.view.*
import java.math.BigDecimal

/**
 * @author : wealive
 * @date   : 2020/4/10 11:46
 * @desc   :
 */
class AudioListAdapter(layoutResId: Int, data: MutableList<LocalMedia>) :
        BaseQuickAdapter<LocalMedia, BaseViewHolder>(layoutResId, data) {
    @SuppressLint("SetTextI18n")
    @RequiresApi(Build.VERSION_CODES.O)
    override fun convert(helper: BaseViewHolder, item: LocalMedia?) {
        with(helper.itemView) {
            iv_speaker.setImageResource(R.mipmap.icon_speaker)
            if (item?.path!!.contains("obs")) {
                showDuration(tv_duration, iv_player, item.path)
//                tv_duration.text = getRingDuring(item.path).toString()
//                duration = tv_duration.text.toString().toInt()
            } else {
                val decimal: BigDecimal =
                        BigDecimal(item.duration / 1000).setScale(
                                0,
                                BigDecimal.ROUND_HALF_UP
                        )
                if (decimal.toString() == "0") {
                    tv_duration.text = "1"
                } else {
                    tv_duration.text = decimal.toString()
                }
                val duration = decimal.toInt()
                setPlayerWidth(iv_player, duration)
            }
        }
    }

    private fun setPlayerWidth(iv_player: ImageView, duration: Int) {
        val params = iv_player.layoutParams
        params.width = duration * (ScreenUtils.getScreenWidth(mContext) - DensityUtils.dp2px(mContext, 20f)) / 60
        val minimumWidth = DensityUtils.dp2px(mContext, 50f)
        if (params.width < minimumWidth) {
            params.width = minimumWidth
        }
        iv_player.layoutParams = params
    }

    private fun getRingDuring(mUri: String?): Int {
        var duration = ""
        val mmr = MediaMetadataRetriever()
        try {
            if (mUri != null) {
                var headers: HashMap<String?, String?>? = null
                if (headers == null) {
                    headers = HashMap()
                    headers["User-Agent"] = "Mozilla/5.0 (Linux; U; Android 4.4.2; zh-CN; MW-KW-001 Build/JRO03C) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/1.0.0.001 U4/0.8.0 Mobile Safari/533.1"
                }
                mmr.setDataSource(mUri, headers)
            }
            duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
        } catch (ex: Exception) {
        } finally {
            mmr.release()
        }
        return if (duration.isNotEmpty()) duration.toInt() / 1000 else 1
    }

    private fun showDuration(tv: TextView, iv: ImageView, path: String) {
        object : AsyncTask<String, Int, Int>() {
            override fun doInBackground(vararg params: String?): Int {
                val duration = getRingDuring(path)
                return duration
            }

            override fun onPostExecute(result: Int?) {
                tv.text = result.toString()
                setPlayerWidth(iv, result!!)
            }
        }.execute()
    }
}